
#include "NetworkManager.h"

#include "WorkThreadManager.h"

#include "TcpClient.h"
#include "UdpClient.h"

NetworkManager *NetworkManager::s_Instance = Q_NULLPTR;

NetworkManager::~NetworkManager()
{
	s_Instance = Q_NULLPTR;
}

void NetworkManager::init(void)
{
	TcpClient *tcpClient = new TcpClient;

	connect(tcpClient, &TcpClient::socket_receive, this, &NetworkManager::socket_receive);
	connect(this, &NetworkManager::startNet, tcpClient, &TcpClient::OnStart);

	WorkThreadManager::Instance()->addToNetThread(tcpClient);

	emit startNet();
}

const QByteArray headBytes("\xAA\x55\xAA\x55\xAA\x55\xAA\x55");
const QByteArray tailBytes("\xEE\xFF\xEE\xFF");

void NetworkManager::socket_receive(QByteArray data)
{
	mRevBuf.append(data);

	int bufLen = mRevBuf.length();

	while (1)
	{
		int headIndex = mRevBuf.indexOf(headBytes);

		if (headIndex >= 0)
		{
			if (headIndex + 4 < bufLen)
			{
				int frameLen = *(int *)(mRevBuf.mid(headIndex + 9, 4).data());

				if (frameLen > 0)
				{
					int tailIndex = mRevBuf.indexOf(tailBytes, headIndex + 9);

					if (tailIndex > 0)
					{
						/* 完整的一帧数据 */
						QByteArray frameArray = mRevBuf.mid(headIndex, tailIndex + TAIL_LEN - headIndex - 1);

						emit receiveDepthFrame(frameArray);

						/* 删除帧尾前的数据 */
						mRevBuf.remove(0, tailIndex + TAIL_LEN - 1);
					}
					else
					{
						/* 帧未收全,删除帧头前的数据 */
						mRevBuf.remove(0, headIndex);
						break;
					}
				}
				else
				{
					/* 帧长度出错 */
					int nextHeadIndex = mRevBuf.indexOf(headBytes, headIndex + 9);
					/* 帧尾错误,删除帧尾前的数据 */
					if (nextHeadIndex < 0)
					{
						/* 下一帧头没有找到 */
						mRevBuf.clear();
						break;
					}
					else
					{
						/* 下一帧头前的数据 */
						mRevBuf.remove(0, nextHeadIndex);
					}
				}
			}
		}
		else
		{
			/* 缓冲包中不包含帧头,全清*/
			mRevBuf.clear();
			break;
		}
	}


#if 0
	while (1)
	{
		int headIndex = mRevBuf.indexOf(headBytes);

		if (headIndex >= 0)
		{
			int frameLen = *(int *)(mRevBuf.mid(headIndex + 9, 4).data());

			if (frameLen > 0)
			{
				if ((headIndex + frameLen + HEAD_LEN) <= bufLen)
				{
					int tailIndex = mRevBuf.indexOf(tailBytes);
					tailIndex = tailIndex;

					QByteArray tail = mRevBuf.mid(headIndex + frameLen + HEAD_LEN - TAIL_LEN, TAIL_LEN);
					qDebug() << tail;

					//if (mRevBuf.mid(headIndex + frameLen + HEAD_LEN - TAIL_LEN, TAIL_LEN) == tailBytes)
					if (tail == tailBytes)
					{
						/* 完整的一帧数据 */
						QByteArray frameArray = mRevBuf.mid(headIndex, frameLen + HEAD_LEN);

						emit receiveDepthFrame(frameArray);

						/* 删除帧尾前的数据 */
						mRevBuf.remove(0, headIndex + frameLen + HEAD_LEN);
					}
					else
					{
						/* 帧尾错误,删除下一帧头前的数据 */
						int nextHeadIndex = mRevBuf.indexOf(headBytes, headIndex + 9);
						if (nextHeadIndex < 0)
						{
							/* 下一帧头没有找到 */
							mRevBuf.clear();
							break;
						}
						else
						{
							/* 下一帧头前的数据 */
							mRevBuf.remove(0, nextHeadIndex);
						}
					}
				}
				else
				{
					/* 帧未收全,删除帧头前的数据 */
					mRevBuf.remove(0, headIndex);
					break;
				}
			}
			else
			{
				/* 帧长度出错 */
				int nextHeadIndex = mRevBuf.indexOf(headBytes, headIndex + 9);
				/* 帧尾错误,删除帧尾前的数据 */
				if (nextHeadIndex < 0)
				{
					/* 下一帧头没有找到 */
					mRevBuf.clear();
					break;
				}
				else
				{
					/* 下一帧头前的数据 */
					mRevBuf.remove(0, nextHeadIndex);
				}
			}
		}
		else
		{
			/* 缓冲包中不包含帧头,全清*/
			mRevBuf.clear();
			break;
		}
	}
#endif
}

NetworkManager::NetworkManager(QObject *parent) :
	QObject(parent)
{
	mRevBuf.clear();
}

